package com.android.common.flutter;

import com.android.common.constant.ChannelNameConstant;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.JSONMessageCodec;
import io.flutter.plugin.common.MethodChannel;
import java.util.Objects;

public class FlutterChannel {

    // 创建MethodChannel对象
    private MethodChannel mMethodChannel;
    private BasicMessageChannel mMessageChannel;
    private EventChannel mEventChannel;
    private static FlutterChannel mInstance;
    private FlutterChannel() {
    }

    public static FlutterChannel getInstance(){
        if (mInstance == null){
            mInstance = new FlutterChannel();
        }
        return mInstance;
    }

    public void initFlutterChannel(FlutterEngine flutterEngine){
        mMethodChannel = new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), ChannelNameConstant.METHOD_CHANNEL_NAME);
        mMethodChannel.setMethodCallHandler(new BaseMethodChannelCallHandler());
        mMessageChannel = new BasicMessageChannel<>(Objects.requireNonNull(flutterEngine).getDartExecutor().getBinaryMessenger(),ChannelNameConstant.BASIC_MESSAGE_CHANNEL_NAME, JSONMessageCodec.INSTANCE);
        mMessageChannel.setMessageHandler(new BaseBasicMessageChannelHandler());
        mEventChannel = new EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), ChannelNameConstant.EVENT_CHANNEL_NAME);
        mEventChannel.setStreamHandler(BaseEventChannelStreamHandler.getInstance());
    }


    public MethodChannel getMethodChannel(){
        return mMethodChannel;
    }
    public BasicMessageChannel getBasicMessageChannel(){
        return mMessageChannel;
    }

    public EventChannel getEventChannel(){
        return mEventChannel;
    }
}
